package com.aiju.cash.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.cash.ui.login.manager.LoginCheck;
import com.aiju.utils.T;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-11-10 14:42
 * @version: V1.0
 * @Description: 验证码页面
 */
public class CodeActivity extends BaseTitleActivity {

    public static final int NEW_CODE = 0;
    public static final int FORGET_CODE = 1;

    private TextView vertifyView;

    private EditText phoneEditText, vertifyEditText;

    private LoginApi mLoginApi;

    private int codeType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code);

        codeType = getIntent().getIntExtra("codeType", 0);
        if (codeType == NEW_CODE) {
            setCenterTitle("填写验证码");
        } else {
            setCenterTitle("忘记登录密码");
            findViewById(R.id.ly_new_password).setVisibility(View.VISIBLE);
        }
        mLoginApi = new LoginApi();
        phoneEditText = (EditText) findViewById(R.id.et_code_phone);
        vertifyEditText = (EditText) findViewById(R.id.et_code_vertify);

        vertifyView = (TextView) findViewById(R.id.btn_code_vertify);
        vertifyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                String phone = phoneEditText.getText().toString().trim();
                if (LoginCheck.checkPhone(phone)) {
                    String code = "";
                    if (codeType == NEW_CODE) {
                        code = "1";
                    } else {
                        code = "2";
                    }
                    mDialogHelper.showLoadingProgressDialog();
                    mLoginApi.getAuthCode(phone, code, new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            T.ToastNetError();
                            mDialogHelper.dismissProgressDialog();
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            mDialogHelper.dismissProgressDialog();
                            DataJson dataJson = new DataJson(response);
                            if (dataJson.isSuccess()) {
                                v.setEnabled(false);
                                timer.start();
                                T.ToastShort("验证码已发送，请查收");
                            } else {
                                T.ToastShort(dataJson.getMsg());
                            }
                        }
                    });
                }
            }
        });

        findViewById(R.id.tv_code_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = phoneEditText.getText().toString().trim();
                String code = vertifyEditText.getText().toString().trim();
                if (codeType == NEW_CODE) {
                    onNewCodeNext(phone, code);
                } else {
                    onForgetCodeNext(phone, code);
                }
            }
        });
    }

    /**
     * 倒计时
     */
    private final CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            vertifyView.setText((millisUntilFinished / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            vertifyView.setEnabled(true);
            vertifyView.setText("获取验证码");
        }
    };

    /**
     * 验证码
     */
    private void onNewCodeNext(final String phone, final String code) {
        if (LoginCheck.checkPhone(phone) && LoginCheck.checkCode(code)) {
            mDialogHelper.showLoadingProgressDialog();
            mLoginApi.checkAuthCode(phone, code, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    T.ToastNetError();
                    mDialogHelper.dismissProgressDialog();
                }

                @Override
                public void onResponse(String response, int id) {
                    mDialogHelper.dismissProgressDialog();
                    DataJson dataJson = new DataJson(response);
                    if (dataJson.isSuccess()) {
                        Intent intent = new Intent(CodeActivity.this, RegisterActivity.class);
                        intent.putExtra("userName", phone);
                        startActivity(intent);
                    } else {
                        T.ToastShort(dataJson.getMsg());
                    }
                }
            });
        }
    }

    /**
     * 忘记密码
     */
    private void onForgetCodeNext(final String userName, String code) {
        EditText newEditText = (EditText) findViewById(R.id.et_code_forget);
        final String newPassword = newEditText.getText().toString().trim();
        if (LoginCheck.checkPhone(userName) && LoginCheck.checkCode(code) && LoginCheck.checkPassword(newPassword)) {
            mDialogHelper.showLoadingProgressDialog();
            mLoginApi.setNewPassword(userName, newPassword,code, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    T.ToastNetError();
                    mDialogHelper.dismissProgressDialog();
                }

                @Override
                public void onResponse(String response, int id) {
                    mDialogHelper.dismissProgressDialog();
                    DataJson dataJson = new DataJson(response);
                    if (dataJson.isSuccess()) {
                        //new LoginManager(CodeActivity.this, mDialogHelper, false).login(userName, newPassword);
                        CodeActivity.this.finish();
                        Intent intent = new Intent(CodeActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        T.ToastShort(dataJson.getMsg());
                    }
                }
            });
        }
    }

}
